package com.sl.biz.modular.process.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.biz.modular.process.controller.request.ProcessRelationConfigSaveRequest;
import com.sl.biz.modular.process.controller.request.ProcessRelationConfigUpdateParamByIdRequest;
import com.sl.biz.modular.process.controller.request.ProcessRelationConfigVersionListRequest;
import com.sl.biz.modular.process.controller.response.ProcessRelationVersionListVO;
import com.sl.biz.modular.process.controller.response.ProcessVersionVO;
import com.sl.biz.modular.process.dao.domain.dto.ProcessVersionDTO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessRelationConfigDO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessVersionDO;
import com.sl.biz.modular.process.dao.service.ProcessRelationConfigDaoService;
import com.sl.biz.modular.process.service.ProcessRelationConfigBizService;
import com.sl.biz.modular.process.service.ProcessVersionBizService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ProcessRelationConfigBizServiceImpl implements ProcessRelationConfigBizService {

    private final ProcessRelationConfigDaoService processRelationConfigDaoService;

    private final ProcessVersionBizService processVersionBizService;

    @Override
    public Boolean save(ProcessRelationConfigSaveRequest processRelationConfigSaveRequest) {


        if (StrUtil.isNotBlank(processRelationConfigSaveRequest.getId())) {
            ProcessRelationConfigUpdateParamByIdRequest processRelationConfigUpdateParamByIdRequest = new ProcessRelationConfigUpdateParamByIdRequest();
            processRelationConfigUpdateParamByIdRequest.setId(processRelationConfigSaveRequest.getId());
            processRelationConfigUpdateParamByIdRequest.setParams(processRelationConfigSaveRequest.getParams());
            return this.updateParamsById(processRelationConfigUpdateParamByIdRequest);
        }

        //验证
        ProcessRelationConfigDO processRelationConfigDO = BeanUtil.copyProperties(processRelationConfigSaveRequest, ProcessRelationConfigDO.class);
        return processRelationConfigDaoService.save(processRelationConfigDO);
    }

    @Override
    public Boolean remoteById(String id) {
        return processRelationConfigDaoService.removeById(id);
    }

    @Override
    public Boolean updateParamsById(ProcessRelationConfigUpdateParamByIdRequest updateParamByIdRequest) {

        ProcessRelationConfigDO byId = processRelationConfigDaoService.getById(updateParamByIdRequest.getId());
        byId.setParams(updateParamByIdRequest.getParams());
        return processRelationConfigDaoService.updateById(byId);
    }

    @Override
    public List<ProcessRelationVersionListVO> versionList(ProcessRelationConfigVersionListRequest versionListRequest) {

        String bizId = versionListRequest.getBizId();

        //查询列表
        ProcessVersionDO processVersionDO = new ProcessVersionDO();
        processVersionDO.setProcessId(versionListRequest.getProcessId());
        List<ProcessVersionVO> list = processVersionBizService.list(processVersionDO);
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }

        List<ProcessRelationVersionListVO> processRelationVersionListVOS = BeanUtil.copyToList(list, ProcessRelationVersionListVO.class);

        //查询关联信息
        List<String> collect = list.stream().map(ProcessVersionDO::getProcessId).toList();

        List<ProcessRelationConfigDO> processRelationConfigDOS = processRelationConfigDaoService.queryByProcessIdsAndBizType(versionListRequest.getBizType(), collect);

        if (CollUtil.isEmpty(processRelationConfigDOS)) {
            return processRelationVersionListVOS;
        }

        for (ProcessRelationVersionListVO processRelationVersionListVO : processRelationVersionListVOS) {

            String processId = processRelationVersionListVO.getProcessId();
            String processVersion = processRelationVersionListVO.getProcessVersion();

            ProcessRelationConfigDO processRelationConfigDO = processRelationConfigDOS.stream().filter(v -> {
                return StrUtil.equals(bizId, v.getBizId()) && StrUtil.equals(v.getProcessId(), processId) && StrUtil.equals(v.getProcessVersion(), processVersion);
            }).findFirst().orElse(null);

            if (ObjectUtil.isNull(processRelationConfigDO)) {
                continue;
            }
            processRelationVersionListVO.setRelationId(processRelationConfigDO.getId());
            processRelationVersionListVO.setRelationFlag(true);
            processRelationVersionListVO.setParams(processRelationConfigDO.getParams());
        }

        return processRelationVersionListVOS;
    }

    @Override
    public List<ProcessRelationConfigDO> queryByBizIdAndBizType(String jobId, String bizType) {
        List<ProcessRelationConfigDO> processRelationConfigDOS = processRelationConfigDaoService.queryByBizIdAndBizType(jobId, bizType);
        return processRelationConfigDOS;
    }

    @Override
    public List<ProcessRelationVersionListVO> processAndVersionList(String bizId, String bizType) {
        List<ProcessRelationConfigDO> processRelationConfigDOS = this.queryByBizIdAndBizType(bizId, bizType);

        if (CollUtil.isEmpty(processRelationConfigDOS)) {
            return Collections.emptyList();
        }

        List<ProcessRelationVersionListVO> processRelationVersionListVOS = Lists.newArrayList();
        for (ProcessRelationConfigDO processRelationConfigDO : processRelationConfigDOS) {

            ProcessVersionDO processVersionDO = new ProcessVersionDO();
            processVersionDO.setProcessVersion(processRelationConfigDO.getProcessVersion());
            processVersionDO.setProcessId(processRelationConfigDO.getProcessId());
            ProcessVersionDTO byProcessIdAndVersion = processVersionBizService.getByProcessIdAndVersion(processVersionDO);
            if (ObjectUtil.isNull(byProcessIdAndVersion)) {
                continue;
            }
            ProcessRelationVersionListVO processRelationVersionListVO = BeanUtil.copyProperties(byProcessIdAndVersion, ProcessRelationVersionListVO.class);
            processRelationVersionListVO.setRelationId(processRelationConfigDO.getId());
            processRelationVersionListVO.setRelationFlag(true);
            processRelationVersionListVO.setParams(processRelationConfigDO.getParams());
            processRelationVersionListVOS.add(processRelationVersionListVO);
        }
        return processRelationVersionListVOS;
    }

    @Override
    public Boolean removeByBizAndAndBizType(String bizId, String bizType) {
        return processRelationConfigDaoService.removeByBizAndAndBizType(bizId,bizType);
    }

}
